$.extend(validatePrompt, {
    mail:{
        onFocus:"",
        succeed:"",
        isNull:"Please enter your e-mail address.",
        error:"Please enter a valid e-mail address."
    }
});

$.extend(validateFunction, {
    mail:function(option) {
        var format = validateRules.isNull(option.value);
        var format2 = validateRules.isEmail(option.value);
        if (!format&&format2) {
            validateSettings.succeed.run(option);
        }
        else {
            if(format){
                validateSettings.error.run(option, option.prompts.isNull);
            }

            else if(!format2)
                validateSettings.error.run(option, option.prompts.error);
        }
    },
    pwd:function(option) {
        validateSettings.succeed.run(option);
    },
    FORM_validate:function() {
        $("#loginname").jdValidate(validatePrompt.mail,validateFunction.mail,true);
        $("#loginpwd").jdValidate(validatePrompt.pwd, validateFunction.pwd, true);
        return validateFunction.FORM_submit(["#loginname","#loginpwd"]);
    },
    FORM_validate_authcode:function() {
        $("#loginname").jdValidate(validatePrompt.mail,validateFunction.mail,true);
        $("#loginpwd").jdValidate(validatePrompt.pwd, validateFunction.pwd, true);
        $("#lauthcode").jdValidate(validatePrompt.authcode, validateFunction.authcode, true);
        return validateFunction.FORM_submit(["#loginname","#loginpwd","#lauthcode"]);
    }
});
//setTimeout(function(){$("#loginname").get(0).focus();},0);
setTimeout(function() {
    if (!$("#loginname").val()) {
        $("#loginname").get(0).focus();
    }
}, 0);
$("#loginname").jdValidate(validatePrompt.mail,validateFunction.mail);
$("#loginpwd").jdValidate(validatePrompt.empty, validateFunction.pwd);
$("#lauthcode").jdValidate(validatePrompt.empty, validateFunction.authcode);

function verc1() {
    $("#JD_Verification1").click();
}


/*$("#loginname,#loginpwd, #lauthcode").bind('keyup', function(event) {
    if (event.keyCode == 13) {
        $("#loginsubmit").click();
    }
});*/

$("#loginsubmit").click(function() {
    var flag = validateFunction.FORM_validate();
    if (flag) {

        var srcValue = $("#JD_Verification1").attr("src");
        if (!srcValue) {
            srcValue = $("#JD_Verification1").attr("src2");
        }
        var uuid = srcValue.split("&uid=")[1].split("&")[0];
        $(this).attr({ "disabled": "disabled" });
        $.ajax({
            type: "POST",
            url: "login_l?uuid=" + uuid + "&" + location.search.substring(1) + "&r=" + Math.random(),
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            data: $("#formlogin").serialize(),
            error: function() {
                $("#loginpwd").attr({ "class": "s-textbox highlight2" });
                $("#loginpwd_error").html("system busy.").show().attr({ "class": "error" });
                $("#loginsubmit").removeAttr("disabled");
            },
            success: function(result) {
                if (result) {
                    var obj = eval(result);
                    if (obj.success) {
                        if (typeof SSO == "undefined") {
                            window.location = obj.success;
                        } else {
                            SSO.login(function(url) {
                                window.location = url;
                            }, obj.success);
                        }
                    }
                    if (obj.transfer) {
                        if (typeof SSO == "undefined") {
                            window.location = obj.transfer + window.location.search;
                        } else {
                            SSO.login(function(url) {
                                window.location = url;
                            }, obj.transfer + window.location.search);
                        }
                    }
                    if (obj.verifycode)
                        $("#o-authcode").show();
                    if (obj.username) {
                        $("#loginname").attr({ "class": "s-textbox highlight2" });
                        $("#loginname_error").html(obj.username).show().attr({ "class": "error" });
                    }
                    if (obj.pwd) {
                        $("#loginpwd").attr({ "class": "s-textbox highlight2" });
                        $("#loginpwd_error").html(obj.pwd).show().attr({ "class": "error" });
                    }
                    if (obj.authcode) {
                        $("#lauthcode").attr({ "class": "s-yzbox highlight2" });
                        $("#lauthcode_error").html(obj.authcode).show().attr({ "class": "error" });
                    }
                    $("#loginsubmit").removeAttr("disabled");
                    verc1();
                }
            }
        });
    }
});
$("#loginsubmitframe").click(function() {
    var flag = validateFunction.FORM_validate();
    if (flag) {

        var srcValue = $("#JD_Verification1").attr("src");
        if (!srcValue) {
            srcValue = $("#JD_Verification1").attr("src2");
        }
        var uuid = srcValue.split("&uid=")[1].split("&")[0];

        $(this).attr({ "disabled": "disabled" });
        $.ajax({
            type: "POST",
            url: "LoginService.aspx?uuid=" + uuid + "&" + location.search.substring(1) + "&r=" + Math.random(),
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            data: $("#formloginframe").serialize(),
            error: function() {
                $("#loginpwd").attr({ "class": "s-textbox highlight2" });
                $("#loginpwd_error").html("ϵͳæ�����Ժ�").show().attr({ "class": "error" });
                $("#loginsubmitframe").removeAttr("disabled");
            },
            success: function(result) {
                if (result) {
                    var obj = eval(result);
                    if (obj.success || obj.transfer) {
                        window.parent.jdModelCallCenter.init();
                    }
                    if (obj.verifycode) {
                        verc1();
                        $("#o-authcode").show();
                    }
                    if (obj.username) {
                        $("#loginname").attr({ "class": "s-textbox highlight2" });
                        $("#loginname_error").html(obj.username).show().attr({ "class": "error" });
                    }
                    if (obj.pwd) {
                        $("#loginpwd").attr({ "class": "s-textbox highlight2" });
                        $("#loginpwd_error").html(obj.pwd).show().attr({ "class": "error" });
                    }
                    if (obj.authcode) {
                        verc1();
                        $("#authcode").attr({ "class": "text text-1 highlight2" });
                        $("#authcode_error").html(obj.authcode).show().attr({ "class": "error" });
                    }
                    $("#loginsubmitframe").removeAttr("disabled");
                }
            }
        });
    }
});
$("#loginname,#loginpwd, #authcode").bind('keyup', function(event) {
    if (event.keyCode == 13) {
        $("#loginsubmitframe").click();
    }
});